package com.lengxf.pay.wx.service;

import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpUtil;
import com.lengxf.pay.common.bean.PayOrder;
import com.lengxf.pay.wx.bean.BarcodeReq;
import com.lengxf.pay.wx.bean.BarcodeResult;
import com.lengxf.pay.wx.config.WX_PayConfig;

public record WxBarcodePayService(WX_PayConfig wxPayConfig) {
    private static final String url = "https://api.mch.weixin.qq.com/pay/micropay";


    public BarcodeResult barcodePay(PayOrder payOrder) {
        BarcodeReq barcodeReq = new BarcodeReq();
        barcodeReq.setAppid(wxPayConfig.getAppid());
        barcodeReq.setMchId(wxPayConfig.getMerchantId());
        barcodeReq.setNonceStr(UUID.fastUUID().toString(true));
        barcodeReq.setBody(payOrder.getProductName());
        barcodeReq.setOutTradeNo(payOrder.getOrderNo());
        barcodeReq.setTotalFee(payOrder.getPrice().toString());
        barcodeReq.setSpbillCreateIp(payOrder.getOrderCreateIp());
        barcodeReq.setAuthCode(payOrder.getAuthCode());
        barcodeReq.sign(wxPayConfig.getApiV2Key());
        String result = HttpUtil.post(url, barcodeReq.toXml());
        return BarcodeResult.resultToBean(result);
    }


}
